package com.rpp.service.impl;

import com.rpp.service.HelloService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;

import java.util.Random;

/**
 * @Description
 * @Author rpp
 * @Date 2020/8/16 10:59 上午
 */
@DubboService
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        String ip = RpcContext.getContext().getAttachment("ip");
        System.out.println(String.format("服务端接受到请求-参数: %s, ip: %s", name, ip));
        return "hello b:" + name;
    }

    @Override
    public String methodA() {
        //随机休眠0-100ms
        randomSleep();
        System.out.println("methodA 方法执行。。。");
        return "methodA";
    }

    @Override
    public String methodB() {
        //随机休眠0-100ms
        randomSleep();
        System.out.println("methodB 方法执行。。。");
        return "methodB";
    }

    @Override
    public String methodC() {
        //随机休眠0-100ms
        randomSleep();
        System.out.println("methodC 方法执行。。。");
        return "methodC";
    }

    private void randomSleep() {
        int i = new Random().nextInt(100);
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
